package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.bo.UserFamilyBo;
import com.ruoyi.user.domain.vo.UserFamilyVo;
import com.ruoyi.user.service.IUserFamilyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 家庭成员
 *
 * @author strongme
 * @date 2023-01-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/family")
public class UserFamilyController extends BaseController {

    private final IUserFamilyService iUserFamilyService;

    /**
     * 查询家庭成员列表
     */
    @GetMapping("/list")
    public TableDataInfo<UserFamilyVo> list(UserFamilyBo bo, PageQuery pageQuery) {
        return iUserFamilyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出家庭成员列表
     */
    @Log(title = "家庭成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserFamilyBo bo, HttpServletResponse response) {
        List<UserFamilyVo> list = iUserFamilyService.queryList(bo);
        ExcelUtil.exportExcel(list, "家庭成员", UserFamilyVo.class, response);
    }

    /**
     * 获取家庭成员详细信息
     *
     * @param familyId 主键
     */
    @GetMapping("/{familyId}")
    public R<UserFamilyVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long familyId) {
        return R.ok(iUserFamilyService.queryById(familyId));
    }

    /**
     * 根据人员ID获取家庭成员列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/listByUserId/{userId}")
    public R<List<UserFamilyVo>> getInfosByUserId(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long userId) {
        return R.ok(iUserFamilyService.queryByUserId(userId));
    }

    /**
     * 新增家庭成员
     */
    @Log(title = "家庭成员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserFamilyBo bo) {
        return toAjax(iUserFamilyService.insertByBo(bo));
    }

    /**
     * 修改家庭成员
     */
    @Log(title = "家庭成员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserFamilyBo bo) {
        return toAjax(iUserFamilyService.updateByBo(bo));
    }

    /**
     * 删除家庭成员
     *
     * @param familyIds 主键串
     */
    @Log(title = "家庭成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] familyIds) {
        return toAjax(iUserFamilyService.deleteWithValidByIds(Arrays.asList(familyIds), true));
    }
}
